package com.cxs.plan.config;

import com.alibaba.fastjson.JSON;

import com.cxs.plan.constant.HttpStatus;
import com.cxs.plan.domain.HttpResult;
import com.cxs.plan.utils.WebUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义授权失败异常
 */
@Component
public class RestAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        HttpResult result = new HttpResult(HttpStatus.FORBIDDEN, "权限不足", null);
        String json = JSON.toJSONString(result);
        WebUtils.renderString(response,json);
    }
}
